﻿using System;

namespace com.hyrentals.common
{
    public static class FormatHelper
    {
        public static string FormatAsDate(DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd");
        }

        public static string FormatAsTimeSpan(DateTime dt)
        {
            //DateTime now = DateTime.Now;
            //if (dt > now)
            //    return dt.ToString("yyyy-MM-dd");

            //TimeSpan ts = now - dt;
            //if (ts.TotalHours <= 1.0d)
            //{
            //    if (ts.TotalMinutes >= 1.0d)
            //        return string.Format("{0}分钟前", (int)ts.TotalMinutes);
            //    else if (ts.TotalSeconds >= 1.0d)
            //        return string.Format("{0}秒前", (int)ts.TotalSeconds);
            //    else
            //        return "1秒前";
            //}
            //if (ts.TotalHours > 1.0d && ts.TotalHours <= 24.0d)
            //    return string.Format("{0}小时前", (int)ts.TotalHours);
            //if (ts.TotalDays > 1.0d && ts.TotalDays <= 7.0d)
            //    return string.Format("{0}天前", (int)ts.TotalDays);

            //return dt.ToString("yyyy-MM-dd");

            DateTime now = DateTime.Now;
            DateTime today = new DateTime(now.Year, now.Month, now.Day);
            DateTime yestoday = today.Subtract(new TimeSpan(24, 0, 0));
            if (dt > now)
            {
                if (dt.Minute == 0 && dt.Hour == 0)
                {
                    return dt.ToString("yyyy-MM-dd");
                }
                else
                {
                    return dt.ToString("yyyy-MM-dd HH:mm");
                }
            }

            TimeSpan ts = now - dt;
            if (ts.TotalHours <= 1.0d)
            {
                if (ts.TotalMinutes >= 1.0d)
                    return string.Format("{0}分钟前", (int)ts.TotalMinutes);
                else
                    return "1分钟前";
            }
            if (dt >= today)
                return dt.ToString("今天 HH:mm");
            if (dt >= yestoday)
                return dt.ToString("昨天 HH:mm");

            if (dt.Minute == 0 && dt.Hour == 0)
            {
                return dt.ToString("yyyy-MM-dd");
            }
            else
            {
                return dt.ToString("yyyy-MM-dd HH:mm");
            }

        }

        public static string FormatAsDateRange(DateTime start, DateTime end)
        {
            if (start == end)
            {
                return start.ToString("yyyy-MM-dd");
            }
            else
            {
                return string.Format("{0} - {1}", start.ToString("yyyy-MM-dd"), end.ToString("yyyy-MM-dd"));
            }
        }
    }
}
